(function ($) {
	var pageType,source,perInfo,postData;
	var wardID = ''
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getStatData(){
		mui('#scroll .areaCard').each(function (index,element) {
			element.removeEventListener('tap',areaCardTapped)
		})
		//parr 1^2022-06-01^2022-06-30^N1293
		var startDay = $('#startDate')[0].innerText
		var endDay = $('#endDate')[0].innerText
		if (startDay > endDay) {
			mui.toast('开始日期不能晚于结束日期')
			return
		}
		var parr = wardID+'^'+startDay+'^'+endDay+'^'+perInfo.loginCode
		var pdict = {'parr':parr,'nurseid':perInfo.loginID}
		var paramDict = {'method':'web.INMLeaveComm:FindLeaveLongList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result['data']
			if (result['msgcode'] != '1') {
				showNullTip(result.msg)
				$('#typeNum')[0].innerText = ""
				$('#scroll')[0].innerHTML = ''
				return 
			}
			updateUI(dataArr[0])
		}, function(errorStr) {
			$('#typeNum')[0].innerText = ""
			$('#scroll')[0].innerHTML = ''
			showNullTip(errorStr)
		});
	}
	function updateUI(dataDict){
		if (dataDict == undefined) {
			$('#typeNum')[0].innerText = ""
			$('#scroll')[0].innerHTML = ''
			showNullTip("暂无数据")
			return;
		}
		
		$('#typeNum')[0].innerText = dataDict['LeaveTotalTime']
		var innerHtml = ''
		for (var i = 0; i < postData.length;i++) {
			var oneData = postData[i];
			var rw = oneData['rw']
			var num = dataDict[rw]
			innerHtml +='<div class="areaCard" data-rw="'+rw+'">'+
					'<div class="cardTitle mui-ellipsis">'+oneData['PostDesc']+'</div><div class="cardNum">'+num+'</div>'+
				'</div>'
		}
		$('#scroll')[0].innerHTML = innerHtml;
		mui('#scroll .areaCard').each(function (index,element) {
			element.addEventListener('tap',areaCardTapped)
		})
		if (innerHtml == '') {
			showNullTip("暂无数据")
		}else{
			hideNullTip()
		}
	}
	function areaCardTapped(){
		var stDate = $('#startDate')[0].innerText
		var endDate = $('#endDate')[0].innerText
		var rw = this.getAttribute('data-rw')
		var staticText = this.querySelector('.cardTitle').innerText
		var webview = plus.webview.create('pmLeaveApply.html', 'pmLeaveApply', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'title':pageType,'typeId':rw,stDate:stDate,endDate:endDate,staticText:staticText});
		webview.show('fade-in');
	}
	//type P:排班 H:休假 S:特殊 意愿排班传P即
	function getPostData(){
		var applyType = $('#applyType')[0]
		var pdict = {"start":0,'limit':"",'parr':'^'+wardID+'^H'} //P:排班 H:休假 S:特殊
		var paramDict = {'method':'web.INMDBComm:FindWardPostList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result['msgcode'] != '1') {
				mui.toast('请重试')
				return
			}
			postData = result['data']
			getStatData()
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	//选择日期
	function selectDate(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.innerText = dateStr;
			_self.classList.remove('noTextTip')
			if (!postData) {
				getPostData()
			}else{
				getStatData()
			};
		}, function(e) {}, {});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		pageType = self.title
		var typeTitle = $('#typeTitle')[0]
		typeTitle.innerText = '个人请假统计'
		perInfo = app.getState()
		wardID = perInfo['loginWardId']
		switch (pageType){
			case '请假申请':
				source="N"
				break;
			case '护士长请假':
				source="H"
				break;
			default:
				break;
		}
		var deviceDay = getCurrentDeviceDay();
		var ymStr = deviceDay.substring(0,7)
		var startDay = ymStr + '-01';
		var ymdDict=getYMDWithStr(ymStr)
		var endDay =  ymStr + '-'+getMonthMaxDay(ymdDict['year'],ymdDict['month'])
		$('#startDate')[0].innerText = startDay
		$('#endDate')[0].innerText = endDay
		// 日期
		mui('.statDate').on('tap','.dateType',selectDate);
		mui('body').on('tap','#closeBtn',function(){
			mui.back()
		})
		getPostData()
	}
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		mui('.nullData')[0].style.display = 'none';
	}
})(mui)

/*
[{"LeaveNurse":"2","LeaveNurseName":"张芮菲","LeaveNurseLevel":"N0","LeaveWard":"1","LeaveWardDesc":"CCU",
"5":"1天","46":"1天","47":"1天","LeaveTotalTime":"3天"}]
*/